#include "atsha204_read_sn.h"
#include <string.h>
#include "sha204_lib_return_codes.h"
#include "sha204_comm_marshaling.h"
#include "atsha204_defines.h"
#include "sha204_helper.h"



uint8_t atsha204_read_sn(uint8_t *ptr_SN)
{
	static uint8_t sha204_lib_return = SHA204_SUCCESS;			//!< Function execution status, initialized to SUCCES and bitmasked with error codes as needed.
	uint8_t transmit_buffer[SHA204_CMD_SIZE_MAX] = {0};	//!< Transmit data buffer
  uint8_t response_buffer[SHA204_RSP_SIZE_MAX] = {0};	//!< Receive data buffer
	uint8_t wakeup_response_buffer[SHA204_RSP_SIZE_MIN] = {0};
	
	sha204_lib_return |= sha204c_wakeup(wakeup_response_buffer);

	if(SHA204_SUCCESS!=sha204_lib_return)
	{
		return sha204_lib_return;
	}

	//struct sha204_read_parameters read_parameters;
	sha204_lib_return |= sha204m_read(transmit_buffer, response_buffer, SHA204_ZONE_CONFIG | SHA204_ZONE_COUNT_FLAG, CONFIG_BLOCK_0_ADDRESS);	//read 32 bytes
	if(SHA204_SUCCESS!=sha204_lib_return)
	{
		return sha204_lib_return;
	}
	

	// copy data
	memcpy(ptr_SN , &response_buffer[SHA204_BUFFER_POS_DATA], 4);
	memcpy(&ptr_SN[4] , &response_buffer[SHA204_BUFFER_POS_DATA + 8], 5);
	sha204p_sleep();

	return sha204_lib_return;
}
